/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_set
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>

#include "jh_set.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
//数据类型为int，按从小到大的方式排序：
int compare(const void* a, const void* b) {
     return ( *(int*)a - *(int*)b );
}

int main(int argc, char* argv[])
{
    //定义数据结构
    jh_set_t set;
    int buf[] = {7, 5, 8, 2, 3, 1, 0, 9, 4, 6};
    
    //集合初始化 
    jh_set_init(&set, sizeof(int), compare);
    
    //在集合中插入数据
    for(int i = 0; i < sizeof(buf)/sizeof(int); i++) {
        jh_set_insert(&set, &buf[i]);
    }
    
    int val;
    void* usernode;
    //在集合中查找第一个数据节点
    usernode = jh_set_begin(&set);
    //正向遍历打印出所有数据，会按照从小到大的顺序打印出来
    JH_SET_ITER(&set, usernode, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    val = 5;
    //在集合中查找数据5
    usernode = jh_set_find(&set, &val);
    //从数据5开始正向遍历打印出后续的数据
    JH_SET_ITER(&set, usernode, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //在集合中查找最后一个数据节点
    usernode = jh_set_end(&set);
    //反向遍历打印出所有数据，会按照从大到小的顺序打印出来
    JH_SET_ITER_REVERSE(&set, usernode, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //获取集合的数据数量与判断集合是否为空
    printf("count %ld, set %s empty\n", jh_set_count(&set), jh_set_is_empty(&set) ? "is" : "isn't");
    
    //删除集合中的部分数据
    jh_set_erase(&set, &buf[2]);
    jh_set_erase(&set, &buf[1]);
    jh_set_erase(&set, &buf[0]);
    
    //获取集合的数据数量与判断集合是否为空
    printf("count %ld, set %s empty\n", jh_set_count(&set), jh_set_is_empty(&set) ? "is" : "isn't");
    
    //在集合中查找第一个数据节点
    usernode = jh_set_begin(&set);
    //正向遍历打印出所有数据
    JH_SET_ITER(&set, usernode, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //清空集合
    jh_set_clear(&set);
    
    //获取集合的数据数量与判断集合是否为空
    printf("count %ld, set %s empty\n", jh_set_count(&set), jh_set_is_empty(&set) ? "is" : "isn't");
    
    return 0;
}